package com.formula.datastructure.question.tree;

// 获得树的大小
public class Quest04 {
    public static void main(String[] args) {
        Node04 root = new Node04(1);
        Node04 a1 = new Node04(42);
        Node04 a2 = new Node04(45);
        Node04 b1 = new Node04(39);
        Node04 b2 = new Node04(31);
        Node04 b3 = new Node04(35);
        Node04 b4 = new Node04(34);

        root.leftChild = a1;
        root.rightChild = a2;

        a1.leftChild = b1;
        a1.rightChild = b2;
        a2.leftChild = b3;
        a2.rightChild = b4;

        Tree04 tree = new Tree04(root);
        System.out.println(tree.getSize());
    }
}

class Tree04 {
    private Node04 root;

    public Tree04(Node04 node) {
        root = node;
    }

    public int getSize() {
        int count = 0;
        if (root != null) {
            return root.getSize(count);
        }
        return 0;
    }
}

class Node04 {
    public Integer data;
    public Node04 leftChild;
    public Node04 rightChild;

    public Node04(Integer data) {
        this.data = data;
    }

    public int getSize(int count) {
        ++count;
        if (leftChild != null) {
            count = leftChild.getSize(count);
        }
        if (rightChild != null) {
            count = rightChild.getSize(count);
        }
        return count;
    }
}